<?php

/**
 * @file
 * Adds event type to the database
 *
 * Copyright 2011 by UNHM CIS605 project Donate team.
 * This program is part of BBBS and YWCA Donation management system.
 * Donate is free software. It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
 *
 * @version March 7, 2011
 * @author Laura Cassella
 */

/**
 * Functions to create, add, update, delete, get, getall and select from the
 * EventType table in the database. This table is used with the dbEventType class.
 */

include_once('dbConnect.php');
include_once('../model/EventType.php');

Class dbEventType{

	/**
	 * Creates the EventType table in the database.
	 * @return TRUE if table creation succeeds. FALSE, otherwise.
	 */
	static function createTable() {

		$query =("CREATE TABLE eventType
            (
              eventTypeID      INT          NOT NULL AUTO_INCREMENT,
              eventName        VARCHAR(50)  NOT NULL ,
              eventDescription VARCHAR(200) NULL ,
              CONSTRAINT pk_eventType PRIMARY KEY (eventTypeID)
            );
          ");

		connect();
		$result = mysql_query($query);
		echo mysql_error();
		mysql_close();
		if ($result == false) {
			return false;
		}

		//Previous query was successful.
		return true;
	}


	static function addEventTypeByFields($name, $description, &$id)
	{
		$et = new EventType($name, $description);
		$result = dbEventType::addEventType($et, $id);
		if ($result == false) {
			die ("EventType could not be updated with specified information.<br />");
		}
	}

	static function verifyInsertedEventType($id)
	{
		$output = "";
		$error = dbEventType::getAllByID($id, $output);
		if ($error == true) {
			printf("%s<br />", $output);
		}
		else {
	  $ind = new EventType($output['eventName'], $output['eventDescription']);
	  printf("Inserted: EventType with ID=%d has %s.<br />", $id, $ind->printAllAttributes( ));
		}
	}

	/**
	 * Adds an eventType tinstance to the ventType table.
	 * @param int $et the auto-generated eventTypeID ???????
	 */
	static function addEventType($v, &$id) {
		if (!$v instanceof eventType) {
			echo ("Invalid arguement insertInto ( ) function<br />");
			return false;
		}
		//Compose the INSERT INTO query srting. Primary key value is missing.
		//Database server automatically generates a unique ID for the primary Key.
		$query = sprintf("INSERT INTO eventType VALUES(null, '%s', '%s')",
		$v->getEventName(),
		$v->getEventDescription()
		);
		//printf("in addEventType() query is: %s", $query);
		connect( );
		$result = mysql_query($query);
		$id = mysql_insert_id( );
		mysql_close( );
		if ($result == false) {
			echo mysql_error();
			return false;
		}
		//query was successful.
		//printf("EventType added has id %d\n<br />", mysql_insert_id());
		return true;
	}

	/**
	 * Remove an event type from the dbEventType table.
	 * @return TRUE or FALSE. FALSE on failure.
	 */
	function deleteEventType($et) {
		connect();
		$query = 'SELECT * FROM dbEvent WHERE eventTypeID = "' . $eventTypeID . '"';
		$result = mysql_query($query);
		if ($result == null || mysql_num_rows($result) == 0) {
			mysql_close();
			return false;
		}
		$query = 'DELETE FROM dbEvent WHERE eventTypeID = "' . $eventTypeID . '"';
		mysql_close();
		return true;
	}

	/**
	 * Get a single row from the dbEventType table matching a particular eventTypeID.
	 * @return TRUE or FALSE. FALSE on failure.
	 */
	function getEventTypeID($et) {
		connect();
		$query = 'SELECT * FROM dbEventType WHERE eventTypeID = "' . $eventTypeID . '"';
		$result = mysql_query($query);
		if ($result == null || mysql_num_rows($result) !== 1) {
			mysql_close();
			return false;
		}
		mysql_close();
		return $result;
	}

	/**
	 * Get all rows from the dbEventType table ordered by eventName.
	 * @return TRUE or FALSE. FALSE on failure.
	 */
	function getallEvents($en) {
		connect();
		$query = "SELECT * FROM dbEventType ORDER BY eventName";
		$result = mysql_query($query);
		if ($result == null || mysql_num_row($result) == 0) {
			mysql_close();
			return false;
		}
		mysql_close();
		return $result;
	}

	/**
	 * Updates an event name in the database by deleting it (if it exists) and then replacing it.
	 * @param string $en replace name.
	 */
	function updatedbEventType($en) {
		connect();
		$query = "UPDATE dbEventType " .
            "SET eventName = '' " .
            "WHERE eventName = ''";
		$result = mysql_query($query);
		mysql_close();
	}

	/**
	 * Updates an event description in the database by deleting it (if it exists) and then replacing it.
	 * @param string $ed replace description.
	 */



	/**
	 * Selects all event types from the database for a given event name.
	 * @param string $en is an event name.
	 * @return a result set or false.
	 */
	function selectdbEventType($en) {
		connect();
		$query = "SELECT * FROM dbEventType WHERE =\"" . $eventName . "\"";
		$result = mysql_query($query);
		if (!$result) {
			echo 'Could not run query: ' . mysql_error();
			mysql_close();
			return false;
		}
		mysql_close();
		return true;
	}


	public static function getAllByID($id, &$output) {
		$query = sprintf("SELECT eventName, eventDescription FROM eventType WHERE eventTypeID=%d",
		$id);
		// printf("->Query: %s<br />", $query);

		connect( );
		$result = mysql_query($query);
		mysql_close( );

		if ($result == false) {
			$output = sprintf("%s", mysql_error());
			return false;
		}
		//process result and print out warning or attribute values
		return dbEventType::checkForOneInstance($result, $id, $output);
	}

	private static function checkForOneInstance($result, $id, &$output)
	{
		//Local variables are initialized.
		//Think positively about what this checking returns :-) and set $errorStatus
		//to initial value false
		$errorStatus = false;

		$howMany = mysql_num_rows($result);
		// printf("-->Found %d instance(s) for donorID=%d.<br />", $howMany, $id);
		switch ($howMany) {
			case 0:
				$output = sprintf("No eventType instance with ID=%d was found.",
                          $id);
        $errorStatus = true;
        break;
      case 1:
        //mysql_fetch( ) DOES NOT return primary key values!
        $output = mysql_fetch_assoc($result);
        $errorStatus = false;
        break;
      default:
        $output = sprintf("There are multiple eventType instances 
                           with ID=%d.", $id);
        $errorStatus = true;
        break;
    }
    return $errorStatus;
  }

}
?>

